//
//  MyCollectionViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/22.
//  Copyright © 2017年 Lwx. All rights reserved.
//
#define topBGViewH 40
#define bottomLineViewX 10

#import "MyCollectionViewController.h"
#import "MyCollectionViewCell.h"
#import "MyCollectionModel.h"
#import "TravelInforViewController.h"
#import "ActivityDetailViewController.h"
#import "TravelsViewController.h"
@interface MyCollectionViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView * myCollectionView;
@property (nonatomic, strong)UIView * topBGView;
@property (nonatomic, strong)UIView * bottomLineView;
@property (nonatomic, strong)MyCollectionModel * model;
@property (nonatomic, strong)NSString * tidStr;
@property (nonatomic, assign)int rowNum;
@property (nonatomic, strong)NSMutableArray * dataArr;
@end

@implementation MyCollectionViewController(Networking)
- (void)getCollectionListWithType:(int)type{
    NSDictionary * dic = @{
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token,
                           @"type":[NSNumber numberWithInt:type]
                           };

    WS(ws)
    [[HUDHelper sharedInstance]syncLoading:@"翻箱中..."];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/member/Mycollect") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [[HUDHelper sharedInstance]syncStopLoading];
        if (error)
        {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000)
            {
                ws.model = [MyCollectionModel yy_modelWithDictionary:obj];
                [ws.dataArr removeAllObjects];
                [ws.dataArr addObjectsFromArray:ws.model.data];

                [ws.myCollectionView reloadData];
            }
            else
            {
                [ws.dataArr removeAllObjects];
                [ws.myCollectionView reloadData];
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];
}

@end

@implementation MyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tidStr = nil;
    self.dataArr = [NSMutableArray array];
    self.view.backgroundColor = getColor(bgColor);
    [self.view addSubview:self.topBGView];
    [self.view addSubview:self.myCollectionView];
    self.title = @"我的收藏";
    
    UIButton * travelInforBtn = (UIButton *)[self.view viewWithTag:1000];
    [travelInforBtn setTitle:@"旅游信息" forState:UIControlStateNormal];
    UIButton * activityIforBtn = (UIButton *)[self.view viewWithTag:1001];
    [activityIforBtn setTitle:@"活动信息" forState:UIControlStateNormal];
    UIButton * travelNotesBtn = (UIButton *)[self.view viewWithTag:1002];
    [travelNotesBtn setTitle:@"游记" forState:UIControlStateNormal];
    
    int bottomLineViewH = 3;
    _bottomLineView = [[UIView alloc] init];
    _bottomLineView.frame = CGRectMake(bottomLineViewX, travelInforBtn.bottom - bottomLineViewH, travelInforBtn.width - 2 * bottomLineViewX, bottomLineViewH);
    _bottomLineView.backgroundColor = [UIColor orangeColor];
    [travelInforBtn addSubview:_bottomLineView];
    
    self.rowNum = 1;
    [self getCollectionListWithType:self.rowNum];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UICollectionView *)myCollectionView{
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * myCollectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, topBGViewH + 5, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:myCollectionViewFlowLayout];
        _myCollectionView.backgroundColor = getColor(bgColor);
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        [_myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"myCollectionCell"];
        
        myCollectionViewFlowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 2, 200);
        
        myCollectionViewFlowLayout.sectionInset = UIEdgeInsetsZero;
        myCollectionViewFlowLayout.minimumInteritemSpacing = 0;
        myCollectionViewFlowLayout.minimumLineSpacing = 5;
        myCollectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
    }return _myCollectionView;
}

- (UIView *)topBGView{
    if (!_topBGView) {
        _topBGView = [[UIView alloc] init];
        _topBGView.backgroundColor = [UIColor whiteColor];
        _topBGView.frame = CGRectMake(0, 0, SCREEN_WIDTH, topBGViewH);
        int btnW = (SCREEN_WIDTH - 2) / 3;
        for (int i = 0; i<3; i++) {
            UIButton * btn = [[UIButton alloc] init];
            btn.frame = CGRectMake((btnW + 1)*i, 0, btnW, _topBGView.height);
            btn.tag = 1000 + i;//三个按钮的tag值从1000开始
            btn.titleLabel.font = DEF_FontSize_15;
            [btn setTitleColor:getColor(@"6d6d6d") forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(collectionReloadClick:) forControlEvents:UIControlEventTouchUpInside];
            [_topBGView addSubview:btn];
            UIView * lineView = [[UIView alloc] init];
            lineView.frame = CGRectMake(btn.right + 1, 3, 1, _topBGView.height - 2 * 3);
            lineView.backgroundColor = getColor(bgColor);
            [_topBGView addSubview:lineView];
        }
    }return _topBGView;
}

- (void)collectionReloadClick:(UIButton *)sender{
    if (sender.tag == 1000) {
        WS(ws)
        [UIView animateWithDuration:0.3 animations:^{
            ws.bottomLineView.x = sender.x + bottomLineViewX;
        }];
        self.rowNum = 1;
        [self getCollectionListWithType:self.rowNum];
    }else if (sender.tag == 1001){
        WS(ws)
        [UIView animateWithDuration:0.3 animations:^{
            ws.bottomLineView.x = sender.x + bottomLineViewX;
        }];
        self.rowNum = 2;
        [self getCollectionListWithType:self.rowNum];
    }else if (sender.tag == 1002){
        WS(ws)
        [UIView animateWithDuration:0.3 animations:^{
            ws.bottomLineView.x = sender.x + bottomLineViewX;
        }];
        self.rowNum = 3;
        [self getCollectionListWithType:self.rowNum];
    }
}

#pragma collectionDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionCell" forIndexPath:indexPath];
    cell.modelData = self.dataArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"myCollectionCell";
    MyCollectionViewCell * cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.modelData = self.model.data[indexPath.row];
    _tidStr = cell.modelData.tid;
    NSLog(@"%@", _tidStr);
    
    if (self.rowNum == 1) {
        TravelInforViewController * travelInforVC = [[TravelInforViewController alloc] init];
        travelInforVC.tid = [_tidStr intValue];
        [self.navigationController pushViewController:travelInforVC animated:YES];
    }
    if (self.rowNum == 2) {
        ActivityDetailViewController * activityDetailVC = [[ActivityDetailViewController alloc] init];
        activityDetailVC.activityId = [_tidStr intValue];
        [self.navigationController pushViewController:activityDetailVC animated:YES];
        
    }
    if (self.rowNum == 3) {
        TravelsViewController * travelsVC = [[TravelsViewController alloc] init];
        travelsVC.dID = [_tidStr intValue];
        [self.navigationController pushViewController:travelsVC animated:YES];
    }
    
}

@end
